<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品筛选器操作类
 * @package  htdocs
 * @subpackage  module
 * @author   allen.qiang
 * @version 1.0
 */

class gSelectorHandler {

    /**
     * @var object 数据库实例
     */
    private static $dbca;

    /**
     * @var string 筛选器规则
     */
    private static $selector_rule;

    /**
     * @var int 筛选器ID
     */
    private static $selectID;

    /**
     * @var int 三级网购号
     */
    private static $postID;

    public function __construct( &$dbca ) {
        self::$dbca = $dbca;
    }

    /**
     * 查询筛选器信息
     * @return bool
     */
    private function searchSelector() {

        $search_sql = 'SELECT * FROM goods_selector WHERE selectID = ?';
        $selector_array = self::$dbca->stmt_query($search_sql, 'i', self::$selectID);
        if (!$selector_array)
            return false;

        if ($selector_array[0]->selectorType == 1) {
            self::$selector_rule = 'include';
        } else {
            self::$selector_rule = 'exclude';
        }

        return true;

    }

    /**
     * 查询筛选器选择信息
     * @return mixed
     */
    private function searchSelectorDetail() {

        $search_sql = 'SELECT * FROM goods_selector_detail WHERE selectID = ?';
        $selector_detail_array = self::$dbca->stmt_query($search_sql, 'i', self::$selectID);
        if (!$selector_detail_array)
            return null;

        $item_array = null;

        foreach($selector_detail_array as &$item) {
            if (empty($item->selector))
                continue;
            $item_array[$item->selectType][] = $item;
        }

        return $item_array;

    }


    /**
     * 检查商品是否符合商品筛选器
     * @param int $selectID 筛选器ID
     * @param int $postID 三级网购号
     * @return bool
     */
    public function isValid( $selectID, $postID ) {

        if (!isset($selectID) || !is_numeric($selectID))
            return true;

        if (!isset($postID) || !is_numeric($postID))
            return false;

        self::$selectID = $selectID;
        self::$postID = $postID;

        //查询筛选器信息
        if (!$this->searchSelector())
            return false;

        return $this->fitSelector();

    }

    /**
     * 检查商品匹配筛选器选项
     * @return bool
     */
    private function fitSelector() {

        try {

            //查询筛选器选择信息
            $selector_detail_array = $this->searchSelectorDetail();
            if (!$selector_detail_array)
                return false;

            //查询商品信息
            $goods_obj = new entity('goods_sale');
            $goods_obj->postID = self::$postID;
            if (!self::$dbca->fetch($goods_obj))
                return false;

            //符合数量
            $fit_num = 0;

            //检查商品是否符合筛选器选项
            foreach($selector_detail_array as $key => &$detail) {

                //是否在筛选器选项里
                $include = false;

                switch($key) {

                    case 1: //分类筛选

                        $c_string = $goods_obj->bigCategoryID . ',' . $goods_obj->midCategoryID;
                        if (!empty($goods_obj->smallCategoryID))
                            $c_string .= ',' . $goods_obj->smallCategoryID;

                        foreach ($detail as &$item) {
                            if (strpos($c_string, $item->selector) !== false) {
                                $include = true;
                                break;
                            }
                        }

                        break;

                    case 2: //商品编码

                        foreach ($detail as &$item) {
                            if (strtolower($goods_obj->product_code) == strtolower($item->selector)) {
                                $include = true;
                                break;
                            }
                        }

                        break;

                    case 3: //价格范围

                        foreach ($detail as &$item) {
                            $s_array = explode(',', $item->selector);
                            if ($s_array[0] <= $goods_obj->salePrice && $s_array[1] >= $goods_obj->salePrice) {
                                $include = true;
                                break;
                            }
                        }

                        break;

                    case 4: //品牌

                        foreach ($detail as &$item) {
                            if ($goods_obj->brandID == $item->selector) {
                                $include = true;
                                break;
                            }
                        }

                        break;

                    case 6: //关键词

                        foreach ($detail as &$item) {

                            $s_array = explode(',', $item);

                            foreach($s_array as &$goods_key) {

                                $search_sql = 'SELECT postID from goods_keyword WHERE postID = ? AND keyword = ?';
                                if (self::$dbca->stmt_query($search_sql, 'is', self::$postID, $goods_key)) {
                                    $include = true;
                                    break;
                                }

                            }

                            if ($include)
                                break;

                        }

                        break;

                }

                if ($include) {
                    if (self::$selector_rule == 'exclude')
                        return false;
                    ++$fit_num;
                }

            }

            if (self::$selector_rule == 'include' && $fit_num != count($selector_detail_array))
                return false;

            return true;

        } catch (Exception $e) {
            error_log($e->getMessage());
            return false;
        }

    }


}